package com.jy.oms.framework.asyn.job;

import com.jy.oms.framework.init.InitializingService;
import com.jy.oms.framework.thread.CloseableThread;
import org.springframework.context.ApplicationContext;

/**
 * 循环单机任务
 *
 * @author liuquan
 */
public abstract class AbstractLoopStandaloneJob implements IStandaloneJob, InitializingService {

    @Override
    public void initService(ApplicationContext applicationContext) {
        Thread thread = new LoopJobThread();
        thread.setName(this.getClass().getSimpleName()+"_job");
        thread.start();
    }

    private class LoopJobThread extends CloseableThread{

        @Override
        protected void runCloseable() throws Exception {
            executeJob();
        }

        @Override
        protected void afterStop() {
            Thread.currentThread().interrupt();
        }
    }
}
